/**
 * created by 小卷毛, 2021-05-08
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import android.content.Context
import android.text.method.PasswordTransformationMethod
import android.util.Base64
import androidx.core.view.isVisible
import com.lzy.okgo.model.HttpMethod
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityLoginBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.other.RSAUtils
import com.xinnuo.common.extend.condition
import com.xinnuo.common.extend.optNotNull
import com.xinnuo.common.helper.getString
import com.xinnuo.common_ui.utils.clear
import org.json.JSONObject

/**
 * 描 述：登录操作相关
 * 作 者：小卷毛  2021-05-08 09:09
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class LoginViewModel : CommonViewModel() {

    private var mPublicKey: String? = null

    /**
     * 获取公钥
     */
    fun getPublicKey(onResult: () -> Unit) {
        request(
            isLoadingBefore = true,
            isLoadingAfter = false,
            requestMethod = HttpMethod.GET,
            requestUrl = BaseUrl.public_key,
            onSuccess = {
                mPublicKey = it
                onResult.invoke()
            }
        )
    }

    /**
     * 加密内容
     */
    fun encrypt(content: String) =
        mPublicKey?.let {
            RSAUtils.encrypt(
                RSAUtils.getPublicKey(
                    Base64.decode(
                        JSONObject(mPublicKey!!).optNotNull("modulus"),
                        Base64.URL_SAFE or Base64.NO_WRAP
                    ),
                    Base64.decode(
                        JSONObject(mPublicKey!!).optNotNull("exponent"),
                        Base64.URL_SAFE or Base64.NO_WRAP
                    )
                ),
                content
            ).apply {
                replace('+', '-')
                replace('/', '_')
            }
        }.orEmpty()

    /**
     * 切换登录方式控制
     */
    fun checkLoginMode(
        context: Context,
        mode: String,
        binding: ActivityLoginBinding
    ) {
        binding.apply {
            val loginMode = context.getString("loginMode", "phone")
            val loginInput = context.getString("phone")

            // 账号登录
            (mode == "account").let {
                loginName.isVisible = it
                loginPwd.isVisible = it
                loginEye.isVisible = it
            }

            // 验证码登录
            (mode == "phone").let {
                loginAccount.isVisible = it
                loginYzm.isVisible = it
                btYzm.isVisible = it
            }

            when (mode) {
                "account" -> {
                    // 清空输入
                    loginName.requestFocus()
                    loginName.clear()
                    loginPwd.clear()
                    loginCheck.isChecked = false
                    loginHint.text = "账号密码登录"
                    btLogin.text = "账号密码登录"
                    loginSwitch.text = "点击切换验证码登录"

                    // 密码不可见
                    loginPwd.transformationMethod = PasswordTransformationMethod.getInstance()
                    loginEye.setImageResource(R.mipmap.login_eye_close)
                    loginEye.setTag(R.id.empty_view, "close")

                    // 默认值
                    if (mode == loginMode) {
                        loginInput
                            .isNotBlank()
                            .condition {
                                loginName.setText(loginInput)
                                loginName.setSelection(loginName.text.length)
                                loginCheck.isChecked = true
                            }
                    }
                }

                else -> {
                    // 清空输入
                    loginAccount.requestFocus()
                    loginAccount.clear()
                    loginYzm.clear()
                    loginCheck.isChecked = false
                    loginHint.text = "验证码登录"
                    btLogin.text = "验证码登录"
                    loginSwitch.text = "点击切换账号密码登录"

                    // 默认值
                    if (mode == loginMode) {
                        loginInput
                            .isNotBlank()
                            .condition {
                                loginAccount.setText(loginInput)
                                loginAccount.setSelection(loginAccount.text!!.length)
                                loginCheck.isChecked = true
                            }
                    }
                }
            }
        }
    }

}